package com.yxkj.railservicer.util;


import android.app.Activity;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yxkj.railservicer.R;


public class MessageDialog extends BaseDialog {

    private View view;
    private Button confirm;
    private Button cancle;
    private ImageView iv_cancle;
    private TextView dialog_message;
    private TextView dialog_else;
    private Context activity;
    private TextView hint;
    private int Type = -1;

    /****
     *
     * @param context
     * @param message
     * @param type  1-确认，取消的dialog； 2-拒绝接单； 3-拒绝退单
     */
    public MessageDialog(Context context, String message, int type) {
        super(context);
        activity = context;
        this.Type = type;
        if (type == 1) {
            view = inflater.inflate(R.layout.dialog_message_1, null);
        }
        if (type == 2) {
            view = inflater.inflate(R.layout.dialog_message_refuse_1, null);
        }
        if (type == 3) {
            view = inflater.inflate(R.layout.dialog_message_refuse_2, null);
        }
        setContentView(view);
        setMessage(message);
        setCancle(true);
    }

    public MessageDialog(Context context, View view) {
        super(context);
        activity = context;

        setContentView(view);
        windowDeployCenter();
    }

    public MessageDialog setMessage(String contentMsg) {
        dialog_message = (TextView) view.findViewById(R.id.dialog_message);
        dialog_message.setText(contentMsg);
        return this;
    }

    public MessageDialog setTime(String time) {
        hint = (TextView) view.findViewById(R.id.hint);
        hint.setVisibility(View.VISIBLE);
        hint.setText("温馨提示：请您在" + time + "之前将商品准备好");
        return this;
    }

    public MessageDialog setCancle(boolean tag) {
        iv_cancle = (ImageView) view.findViewById(R.id.iv_cancle);
        if (tag) {
            iv_cancle.setVisibility(View.VISIBLE);
            iv_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        } else {
            iv_cancle.setVisibility(View.GONE);
        }
        return this;
    }

    public MessageDialog setPositiveButton(String msg,
                                           OnDialogButtonClickListener onClickListener) {
        confirm = (Button) view.findViewById(R.id.confirm);
        confirm.setText(msg);
        confirm.setTag(onClickListener);
        confirm.setVisibility(View.VISIBLE);
        confirm.setOnClickListener(mListener);
        return this;
    }

    public MessageDialog setNegativeButton(String msg,
                                           OnDialogButtonClickListener onClickListener) {
        cancle = (Button) view.findViewById(R.id.cancle);
        cancle.setText(msg);
        cancle.setTag(onClickListener);
        cancle.setVisibility(View.VISIBLE);
        cancle.setOnClickListener(mListener);
        return this;
    }

    public MessageDialog setPositiveButton(int textId,
                                           OnDialogButtonClickListener listener) {
        String text = context.getResources().getString(textId);
        setPositiveButton(text, listener);
        return this;
    }

    public MessageDialog setNegativeButton(int textId,
                                           OnDialogButtonClickListener listener) {
        String text = context.getResources().getString(textId);
        setNegativeButton(text, listener);
        return this;
    }

    public void showDialog() {
        try {
            windowDeployCenter();
            show();
        } catch (Exception e) {

        }
    }

    public void windowDeployCenter() {
        Window window = getWindow();
        window.setBackgroundDrawableResource(R.color.pub_color_transparent);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.alpha = 1.0f;
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);

        WindowManager windowManager = ((Activity) activity).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (display.getWidth()) * 3 / 4;
        getWindow().setAttributes(lp);
    }

    public interface MsgCallBack {
        public void confirmMsg(MessageDialog d);

        public void cancelMsg(MessageDialog d);
    }
}